package com.hf.eagle.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.NewsInfo;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Pager;

/**
 * 最新消息
 * @author xiaokeheng
 *
 */
public class LastNewsAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.view(mapping, form, request, response);
	}

	/**
	 * 显示最新消息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean lazyForm = (DynaBean) form;
		String id = (String) lazyForm.get("id");
		NewsInfo newsInfo=new NewsInfo();
		newsInfo.setId(Long.parseLong(id));
		NewsInfo news_info=getFacade().getNewsInfoService().getNewsInfo(newsInfo);
		if(news_info!=null){
			news_info.setView_count(news_info.getView_count()+1);
			super.getFacade().getNewsInfoService().modifyNewsInfo(news_info);
		}
		request.setAttribute("news_info", news_info);
		return mapping.findForward("view");
	}
	
	/**
	 * 显示最新消息列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean lazyForm = (DynaBean) form;
		String mod_id = (String) lazyForm.get("mod_id");
		NewsInfo newsInfo=new NewsInfo();
		newsInfo.setMod_id(Long.parseLong(mod_id));
		newsInfo.setIs_del(0);
		Pager pager = (Pager) lazyForm.get("pager");
		Long recordCount = super.getFacade().getNewsInfoService().getNewsInfoCount(newsInfo);
		pager.init(recordCount, pager.getPageSize(), pager.getRequestPage());
		newsInfo.getRow().setFirst(pager.getFirstRow());
		newsInfo.getRow().setCount(pager.getLastRow());

		List<NewsInfo> list = super.getFacade().getNewsInfoService().getNewsInfoPaginatedList(newsInfo);
        request.setAttribute("mod_id", mod_id);
        super.copyProperties(form, newsInfo);
        request.setAttribute("lastNews", list);
		return mapping.findForward("list");
	}

}